home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / stl / stl_numeric.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-08  |  7.9 KB  |  256 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  *
  15.  * Copyright (c) 1996,1997
  16.  * Silicon Graphics Computer Systems, Inc.
  17.  *
  18.  * Permission to use, copy, modify, distribute and sell this software
  19.  * and its documentation for any purpose is hereby granted without fee,
  20.  * provided that the above copyright notice appear in all copies and
  21.  * that both that copyright notice and this permission notice appear
  22.  * in supporting documentation.  Silicon Graphics makes no
  23.  * representations about the suitability of this software for any
  24.  * purpose.  It is provided "as is" without express or implied warranty.
  25.  */
  26.  
  27. /* NOTE: This is an internal header file, included by other STL headers.
  28.  *   You should not attempt to use it directly.
  29.  */
  30.  
  31.  
  32. #ifndef __SGI_STL_INTERNAL_NUMERIC_H
  33. #define __SGI_STL_INTERNAL_NUMERIC_H
  34.  
  35. __STL_BEGIN_NAMESPACE
  36.  
  37. template <class _InputIterator, class _Tp>
  38. _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
  39. {
  40.   __STL_REQUIRES(_InputIterator, _InputIterator);
  41.   for ( ; __first != __last; ++__first)
  42.     __init = __init + *__first;
  43.   return __init;
  44. }
  45.  
  46. template <class _InputIterator, class _Tp, class _BinaryOperation>
  47. _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
  48.                _BinaryOperation __binary_op)
  49. {
  50.   __STL_REQUIRES(_InputIterator, _InputIterator);
  51.   for ( ; __first != __last; ++__first)
  52.     __init = __binary_op(__init, *__first);
  53.   return __init;
  54. }
  55.  
  56. template <class _InputIterator1, class _InputIterator2, class _Tp>
  57. _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
  58.                   _InputIterator2 __first2, _Tp __init)
  59. {
  60.   __STL_REQUIRES(_InputIterator2, _InputIterator);
  61.   __STL_REQUIRES(_InputIterator2, _InputIterator);
  62.   for ( ; __first1 != __last1; ++__first1, ++__first2)
  63.     __init = __init + (*__first1 * *__first2);
  64.   return __init;
  65. }
  66.  
  67. template <class _InputIterator1, class _InputIterator2, class _Tp,
  68.           class _BinaryOperation1, class _BinaryOperation2>
  69. _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
  70.                   _InputIterator2 __first2, _Tp __init, 
  71.                   _BinaryOperation1 __binary_op1,
  72.                   _BinaryOperation2 __binary_op2)
  73. {
  74.   __STL_REQUIRES(_InputIterator2, _InputIterator);
  75.   __STL_REQUIRES(_InputIterator2, _InputIterator);
  76.   for ( ; __first1 != __last1; ++__first1, ++__first2)
  77.     __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
  78.   return __init;
  79. }
  80.  
  81. template <class _InputIterator, class _OutputIterator, class _Tp>
  82. _OutputIterator 
  83. __partial_sum(_InputIterator __first, _InputIterator __last,
  84.               _OutputIterator __result, _Tp*)
  85. {
  86.   _Tp __value = *__first;
  87.   while (++__first != __last) {
  88.     __value = __value + *__first;
  89.     *++__result = __value;
  90.   }
  91.   return ++__result;
  92. }
  93.  
  94. template <class _InputIterator, class _OutputIterator>
  95. _OutputIterator 
  96. partial_sum(_InputIterator __first, _InputIterator __last,
  97.             _OutputIterator __result)
  98. {
  99.   __STL_REQUIRES(_InputIterator, _InputIterator);
  100.   __STL_REQUIRES(_OutputIterator, _OutputIterator);
  101.   if (__first == __last) return __result;
  102.   *__result = *__first;
  103.   return __partial_sum(__first, __last, __result, __VALUE_TYPE(__first));
  104. }
  105.  
  106. template <class _InputIterator, class _OutputIterator, class _Tp,
  107.           class _BinaryOperation>
  108. _OutputIterator 
  109. __partial_sum(_InputIterator __first, _InputIterator __last, 
  110.               _OutputIterator __result, _Tp*, _BinaryOperation __binary_op)
  111. {
  112.   _Tp __value = *__first;
  113.   while (++__first != __last) {
  114.     __value = __binary_op(__value, *__first);
  115.     *++__result = __value;
  116.   }
  117.   return ++__result;
  118. }
  119.  
  120. template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
  121. _OutputIterator 
  122. partial_sum(_InputIterator __first, _InputIterator __last,
  123.             _OutputIterator __result, _BinaryOperation __binary_op)
  124. {
  125.   __STL_REQUIRES(_InputIterator, _InputIterator);
  126.   __STL_REQUIRES(_OutputIterator, _OutputIterator);
  127.   if (__first == __last) return __result;
  128.   *__result = *__first;
  129.   return __partial_sum(__first, __last, __result, __VALUE_TYPE(__first), 
  130.                        __binary_op);
  131. }
  132.  
  133. template <class _InputIterator, class _OutputIterator, class _Tp>
  134. _OutputIterator 
  135. __adjacent_difference(_InputIterator __first, _InputIterator __last,
  136.                       _OutputIterator __result, _Tp*)
  137. {
  138.   _Tp __value = *__first;
  139.   while (++__first != __last) {
  140.     _Tp __tmp = *__first;
  141.     *++__result = __tmp - __value;
  142.     __value = __tmp;
  143.   }
  144.   return ++__result;
  145. }
  146.  
  147. template <class _InputIterator, class _OutputIterator>
  148. _OutputIterator
  149. adjacent_difference(_InputIterator __first,
  150.                     _InputIterator __last, _OutputIterator __result)
  151. {
  152.   __STL_REQUIRES(_InputIterator, _InputIterator);
  153.   __STL_REQUIRES(_OutputIterator, _OutputIterator);
  154.   if (__first == __last) return __result;
  155.   *__result = *__first;
  156.   return __adjacent_difference(__first, __last, __result,
  157.                                __VALUE_TYPE(__first));
  158. }
  159.  
  160. template <class _InputIterator, class _OutputIterator, class _Tp, 
  161.           class _BinaryOperation>
  162. _OutputIterator
  163. __adjacent_difference(_InputIterator __first, _InputIterator __last, 
  164.                       _OutputIterator __result, _Tp*,
  165.                       _BinaryOperation __binary_op) {
  166.   _Tp __value = *__first;
  167.   while (++__first != __last) {
  168.     _Tp __tmp = *__first;
  169.     *++__result = __binary_op(__tmp, __value);
  170.     __value = __tmp;
  171.   }
  172.   return ++__result;
  173. }
  174.  
  175. template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
  176. _OutputIterator 
  177. adjacent_difference(_InputIterator __first, _InputIterator __last,
  178.                     _OutputIterator __result, _BinaryOperation __binary_op)
  179. {
  180.   __STL_REQUIRES(_InputIterator, _InputIterator);
  181.   __STL_REQUIRES(_OutputIterator, _OutputIterator);
  182.   if (__first == __last) return __result;
  183.   *__result = *__first;
  184.   return __adjacent_difference(__first, __last, __result,
  185.                                __VALUE_TYPE(__first),
  186.                                __binary_op);
  187. }
  188.  
  189. // Returns __x ** __n, where __n >= 0.  _Note that "multiplication"
  190. // is required to be associative, but not necessarily commutative.
  191.  
  192.  
  193. template <class _Tp, class _Integer, class _MonoidOperation>
  194. _Tp __power(_Tp __x, _Integer __n, _MonoidOperation __opr)
  195. {
  196.   if (__n == 0)
  197.     return identity_element(__opr);
  198.   else {
  199.     while ((__n & 1) == 0) {
  200.       __n >>= 1;
  201.       __x = __opr(__x, __x);
  202.     }
  203.  
  204.     _Tp __result = __x;
  205.     __n >>= 1;
  206.     while (__n != 0) {
  207.       __x = __opr(__x, __x);
  208.       if ((__n & 1) != 0)
  209.         __result = __opr(__result, __x);
  210.       __n >>= 1;
  211.     }
  212.     return __result;
  213.   }
  214. }
  215.  
  216. template <class _Tp, class _Integer>
  217. inline _Tp __power(_Tp __x, _Integer __n)
  218. {
  219.   return __power(__x, __n, multiplies<_Tp>());
  220. }
  221.  
  222. // Alias for the internal name __power.  Note that power is an extension,
  223. // not part of the C++ standard.
  224.  
  225. template <class _Tp, class _Integer, class _MonoidOperation>
  226. inline _Tp power(_Tp __x, _Integer __n, _MonoidOperation __opr)
  227. {
  228.   return __power(__x, __n, __opr);
  229. }
  230.  
  231. template <class _Tp, class _Integer>
  232. inline _Tp power(_Tp __x, _Integer __n)
  233. {
  234.   return __power(__x, __n);
  235. }
  236.  
  237. // iota is not part of the C++ standard.  It is an extension.
  238.  
  239. template <class _ForwardIter, class _Tp>
  240. void 
  241. iota(_ForwardIter __first, _ForwardIter __last, _Tp __value)
  242. {
  243.   __STL_REQUIRES(_ForwardIter, _Mutable_ForwardIterator);
  244.   __STL_CONVERTIBLE(_Tp, typename iterator_traits<_ForwardIter>::value_type);
  245.   while (__first != __last)
  246.     *__first++ = __value++;
  247. }
  248.  
  249. __STL_END_NAMESPACE
  250.  
  251. #endif /* __SGI_STL_INTERNAL_NUMERIC_H */
  252.  
  253. // Local Variables:
  254. // mode:C++
  255. // End:
  256.